# -*- coding: utf-8 -*-
"""
@Time    : 2024/7/16 16:03 
@Author  : ZhangShenao 
@File    : assert_usage.py 
@Desc    : assert的典型用法

assert经常被用于检查函数的入参、中间计算结果以及返回值

assert的合理使用,可以增加代码的健壮度,同时也方便定位和排查问题

不要用assert来代替try-except异常处理机制
"""


def discount(origin_price: int, discount_rate: float) -> int:
    """
    计算折扣后的价格
    :param origin_price: 原价
    :param discount_rate: 折扣率
    :return: 折扣后的价格
    """

    # 合理使用assert,对函数的入参和结果进行校验
    assert origin_price > 0, 'origin_price must be positive'
    assert 0 < discount_rate < 1, 'discount_rate must be in (0, 1)'
    discount_price = round(origin_price * (1 - discount_rate))
    assert 0 <= discount_price <= origin_price, 'invalid discount_price'
    return discount_price


def merge_two_list(l1, l2) -> list:
    """合并两个list"""

    # 使用assert,对输入参数进行校验
    assert isinstance(l1, list) and isinstance(l2, list), 'input mush be list'

    return l1 + l2


if __name__ == '__main__':
    print(discount(origin_price=100, discount_rate=0.5))
    print(discount(origin_price=100, discount_rate=0.2))
    # print(discount(origin_price=100, discount_rate=-0.1))   # AssertionError: discount_rate must be in (0, 1)
    # print(discount(origin_price=0, discount_rate=0.2))  # AssertionError: origin_price must be positive

    print(merge_two_list([1, 2], [3, 4]))
    # print(merge_two_list(1, 2)) # AssertionError: input mush be list
